package com.cgwx.service.impl;

import com.cgwx.config.BusinessException;
import com.cgwx.entity.*;
import com.cgwx.mapper.FavoritesMapper;
import com.cgwx.mapper.FileMapper;
import com.cgwx.mapper.FolderMapper;
import com.cgwx.mapper.SharedMapper;
import com.cgwx.service.ISharedService;
import com.cgwx.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class SharedServiceImpl implements ISharedService {
    @Resource
    private SharedMapper sharedMapper;

    @Resource
    private FolderMapper folderMapper;

    @Resource
    private FavoritesMapper favoritesMapper;

    @Resource
    private FileMapper fileMapper;

    @Resource
    private IUserService userService;

    @Override
    public List<Shared> getMyShared(String account, String pattern, String column, String sort) {
        Shared shared = new Shared();
        shared.setAccount(account);
        shared.setIsCanceled("false");
        shared.setEntityName(pattern);
        shared.setSharedName(pattern);
        shared.setColumn(column);
        shared.setSort(sort);
        return sharedMapper.getMyShared(shared);
    }

    @Override
    public List<Shared> getSharedMe(String sharedId, String pattern, String column, String sort) {
        Shared shared = new Shared();
        shared.setSharedId(sharedId);
        shared.setEntityName(pattern);
        shared.setName(pattern);
        shared.setSharedName(pattern);
        shared.setSharedName(pattern);
        return sharedMapper.getSharedMe(shared);
    }

    @Override
    public List<Shared> getShared(Shared shared) {
        return sharedMapper.getShared(shared);
    }

    @Override
    @Transactional
    public String cancelSharedById(List<String> ids) {
        boolean result;
        Shared shared;
        for(String id: ids){
            shared = new Shared();
            shared.setId(id);
            shared = sharedMapper.getShared(shared).get(0);
            String sharedId = shared.getSharedId();
            String entityId = shared.getEntityId();
            Favorites favorite = new Favorites();
            favorite.setAccount(sharedId);
            favorite.setEntityId(entityId);
            List<Favorites> favorites = favoritesMapper.getFavorites(favorite);
            if(!favorites.isEmpty()) {
                Favorites favorites1 = favorites.get(0);
                result = favoritesMapper.deleteFavoritesByAccountAndId(favorites1.getAccount(), favorites1.getEntityId());
                if(!result){
                    log.error("取消分享失败!(1)");
                    throw new BusinessException("取消分享失败!");
                }
            }
            result = sharedMapper.cancelSharedById(id);
            if(!result){
                log.error("取消分享失败!(2)");
                throw new BusinessException("取消分享失败!");
            }
        }
        return "取消分享成功!";
    }

    @Override
    @Transactional
    public String deleteSharedById(List<String> ids) {
        for(String id: ids){
            Shared shared = new Shared();
            shared.setId(id);
            List<Shared> shareds = sharedMapper.getShared(shared);
            if(shareds.isEmpty()){
                log.error("deleteSharedById: 不存在该分享id!");
                throw new BusinessException("不存在该分享id!");
            }
            else if(shareds.size() > 1){
                log.error("deleteSharedById: 该分享id不唯一!");
                throw new BusinessException("该分享id不唯一!");
            }
            else{
                shared = shareds.get(0);
                Timestamp time = (Timestamp.valueOf(LocalDateTime.now()));
                shared.setDeleteTime(time);
                boolean result = sharedMapper.updateSharedById(shared);
                if(!result){
                    log.error("删除分享失败!");
                    throw new BusinessException("删除分享失败!");
                }
            }
        }
        return "删除分享成功!";
    }

    @Override
    public String restoreSharedById(List<String> ids) {
        for(String id: ids){
            Shared shared = new Shared();
            shared.setId(id);
            List<Shared> shareds = sharedMapper.getShared(shared);
            if(shareds.isEmpty()){
                log.error("restoreSharedById: 不存在该分享id!");
                throw new BusinessException("不存在该分享id!");
            }
            else if(shareds.size() > 1){
                log.error("restoreSharedById: 该分享id不唯一!");
                throw new BusinessException("该分享id不唯一!");
            }
            else{
                shared = shareds.get(0);
                shared.setDeleteTime(null);
                boolean result = sharedMapper.restoreSharedById(shared);
                if(!result){
                    log.error("分享恢复失败!");
                    throw new BusinessException("分享恢复失败!");
                }
            }
        }
        return "分享恢复成功!";
    }

    @Override
    public String updateSharedById(Shared shared) {
        boolean result = sharedMapper.updateSharedById(shared);
        if(result) return "更新成功!";
        else return "更新失败!";
    }

    @Override
    @Transactional
    public String insertShared(Shared shared) {
        if(shared.getEntityId() == null || shared.getAccount() == null || shared.getSharedId() == null) return "参数不全!";
        if(shared.getEntityId().startsWith("folder")) {
            Folder folder = new Folder();
            folder.setEntityId(shared.getEntityId());
            List<Folder> list = folderMapper.getFolder(folder);
            if(list.isEmpty()){
                log.error("不存在该文件!(1)");
                throw new BusinessException("不存在该文件!");
            }
            else folder = list.get(0);
            shared.setEntityName(folder.getEntityName());
            shared.setEntityRoom(folder.getEntityRoom());
            shared.setName(folder.getOwnerName());
            shared.setType(folder.getType());
            shared.setTag(folder.getTag());
            shared.setFileOrFolder("folder");
            shared.setIsDeleted("false");
            shared.setIsCanceled("false");
            shared.setIsCanceled("false");
            shared.setCreateTime(folder.getCreateTime());
            shared.setUpdateTime(folder.getUpdateTime());
        }
        else if(shared.getEntityId().startsWith("file")) {
            File file = new File();
            file.setEntityId(shared.getEntityId());
            List<File> list = fileMapper.getFile(file);
            if(list.isEmpty()) {
                log.error("不存在该文件!(2)");
                throw new BusinessException("不存在该文件!");
            }
            else file = list.get(0);
            shared.setEntityName(file.getEntityName());
            shared.setEntityRoom(file.getEntityRoom());
            shared.setName(file.getOwnerName());
            shared.setFileOrFolder("file");
            shared.setName(file.getOwnerName());
            shared.setVersionId(file.getVersionId());
            shared.setType(file.getType());
            shared.setTag(file.getTag());
            shared.setFileId(file.getFileId());
            shared.setFileUrl(file.getFileUrl());
            shared.setIsDeleted("false");
            shared.setIsCanceled("false");
            shared.setFileSize(file.getFileSize());
            shared.setCreateTime(file.getCreateTime());
            shared.setUpdateTime(file.getUpdateTime());
        }
        else{
            log.error("entityId错误!");
            throw new BusinessException("entityId错误!");
        }
        UsUser user = new UsUser();
        user.setAccount(shared.getSharedId());
        user = userService.getUser(user).get(0);
        shared.setSharedName(user.getName());
        shared.setId(generateId());
        boolean result = sharedMapper.insertShared(shared);
        if(!result){
            log.error("分享失败!");
            throw new BusinessException("分享失败!");
        }
        else return "分享成功!";
    }

    @Override
    public List<Shared> getDeptShared(String sharedId, String pattern) {
        Shared shared = new Shared();
        shared.setSharedId(sharedId);
        shared.setEntityName(pattern);
        shared.setIsDeptReport("true");
        return sharedMapper.getDeptShared(shared);
    }

    public String generateId() {
        return UUID.randomUUID().toString();
    }
}
